import com.sun.tools.corba.se.idl.constExpr.Or;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.*;

public class OrderingWindows extends JFrame{
    private JPanel cardPanel = new JPanel(new CardLayout());
    private JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 25));
    private FoodPanelInOrder foodPanelInOrder = new FoodPanelInOrder(new FlowLayout(FlowLayout.CENTER, 20, 20));
    private AddressPanelInOrder addressPanelInOrder = new AddressPanelInOrder(new FlowLayout(FlowLayout.CENTER, 20,20));
    private SuccessPanelInOrder successPanelInOrder = new SuccessPanelInOrder(new FlowLayout(FlowLayout.CENTER, 20,20));
    private ClickPanelInPraise clickPanelInPraise = new ClickPanelInPraise(new FlowLayout(FlowLayout.CENTER, 20,20));
    private SuggestPanelInPraise suggestPanelInPraise = new SuggestPanelInPraise(new FlowLayout(FlowLayout.CENTER, 20, 20));
    ArrayList<JButton> buttons= new ArrayList<>();
    String[] panels = {"menuPanel", "foodPanelInOrder", "check", "sign", "delete", "clickPanelInPraise", "exit", "addressPanelInOrder", "successPanelInOrder", "suggestPanelInPraise"};
    LinkedList<OrderForm> orderForms = new LinkedList<>();
    OrderForm currentOrderForm;

    private OrderingWindows(String title){
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0, 10, 500, 450);

        add(cardPanel);

        LoadPanel(cardPanel);
        showPanels("menuPanel");
        setVisible(true);
    }

    public void showPanels(String panelName){
        CardLayout cardLayout = (CardLayout)cardPanel.getLayout();
        cardLayout.show(cardPanel, panelName);
    }

    private void LoadPanel(JPanel panel){
        menuPanel.setBounds(40, 40, 420, 320);
        panel.add(menuPanel, "menuPanel");
        makeMenuPanel();

        panel.add(suggestPanelInPraise, "suggestPanelInPraise");
        makeSuggestPanelInPraise();

        panel.add(clickPanelInPraise,"clickPanelInPraise");
        makeClickPanelInPraise();

        panel.add(foodPanelInOrder, "foodPanelInOrder");
        makeFoodPanelInOrder();

        panel.add(addressPanelInOrder, "addressPanelInOrder");
        makeAddressPanelInOrder();

        panel.add(successPanelInOrder, "successPanelInOrder");
        makeSuccessPanelInOrder();
    }

    private void makeMenuPanel(){
        int width = 190;
        int height = 90;
        Dimension btnSize = new Dimension(width, height);
        Font btnFont = new Font("Monospaced", Font.PLAIN, 24);

        JButton orderButton = new JButton("我要订餐");
        buttons.add(orderButton);

        JButton checkButton = new JButton("查看餐袋");
        buttons.add(checkButton);

        JButton signButton = new JButton("签收订单");
        buttons.add(signButton);

        JButton deleteButton = new JButton("删除订单");
        buttons.add(deleteButton);

        JButton praiseButton = new JButton("我要点赞");
        buttons.add(praiseButton);

        JButton exitButton = new JButton("退出系统");
        buttons.add(exitButton);

        for (JButton button: buttons
             ) {
            button.setPreferredSize(btnSize);
            button.setFont(btnFont);
            if (button.equals(exitButton)){
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });
            } else {
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        showPanels(panels[buttons.indexOf(button) + 1]);
                        foodPanelInOrder.clean();
                    }
                });
            }
            menuPanel.add(button);
        }
    }

    private void makeClickPanelInPraise(){
       clickPanelInPraise.setBtnSuggestListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               showPanels("suggestPanelInPraise");
           }
       });
       clickPanelInPraise.setBtnConfirmListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               clickPanelInPraise.confirm();
               showPanels("menuPanel");
           }
       });
       clickPanelInPraise.setBtnReturnListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               clickPanelInPraise.clean();
               showPanels("menuPanel");
           }
       });
    }

    private void makeSuggestPanelInPraise(){
        suggestPanelInPraise.setBtnConfirmListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SendEmailThread sendEmailThread = new SendEmailThread();
                sendEmailThread.run(suggestPanelInPraise);
            }
        });

        suggestPanelInPraise.setBtnReturnListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels("clickPanelInPraise");
            }
        });
    }

    private void makeFoodPanelInOrder(){

        foodPanelInOrder.setBtnConfirmListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[7]);
                currentOrderForm = new OrderForm();
                for (int i = 1; i < foodPanelInOrder.getMyComponent().size(); i += 2) {
                    JSpinner spn = (JSpinner) foodPanelInOrder.getMyComponent().get(i);
                    if ((int) spn.getValue() > 0) {
                        currentOrderForm.setDish((i - 1) / 2, (int) spn.getValue());
                        successPanelInOrder.setLblFood(FoodMenu.food[(i - 1) / 2]);
                        successPanelInOrder.setLblFoodNum((int) spn.getValue());
                        successPanelInOrder.setLblCostNum(currentOrderForm.getDishPrices((i - 1) / 2));
                    }
                }
            }
        });

        foodPanelInOrder.setBtnReturnListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[0]);
            }
        });
    }

    public void makeAddressPanelInOrder(){

        addressPanelInOrder.setBtnConfirmListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[8]);

                currentOrderForm.setName(addressPanelInOrder.getTfdName().getText(), Integer.valueOf(addressPanelInOrder.getTfdTime().getText()), addressPanelInOrder.getTfdAddress().getText());
                successPanelInOrder.setLblTimeNum(currentOrderForm.getTime());
                successPanelInOrder.setLblFreightNum(currentOrderForm.getFreight());
                successPanelInOrder.setLblTotalNum(currentOrderForm.getSumPrices());

                orderForms.add(currentOrderForm);
                currentOrderForm = null;
            }
        });

        addressPanelInOrder.setBtnReturnListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[1]);
            }
        });
    }

    public void makeSuccessPanelInOrder(){
        successPanelInOrder.setBtnConfirmListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[0]);
            }
        });

        successPanelInOrder.setBtnReturnListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanels(panels[0]);
            }
        });

    }


    public static void main(String[] args) {
//        try{
//            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
//        } catch (Exception e){
//            System.out.println(e.getMessage());
//        }
        new OrderingWindows("吃货联盟订餐系统");
    }
}
